package meadproperty.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

	public static int BUFFER_BYTE = 512;

	private static String BASE_DIR = "";
	private static byte BYTES[] = new byte[BUFFER_BYTE];

	public static void zipDir(String src_dir) throws IOException {
		zipDir(src_dir, src_dir + ".zip");
	}

	/**
	 * 
	 * @param src_dir
	 * @param dest_zip
	 * @throws IOException
	 */
	public static void zipDir(String src_dir, String dest_zip)
			throws IOException {

		FileOutputStream fos = new FileOutputStream(dest_zip);
		ZipOutputStream zout = new ZipOutputStream(fos);
		File file = new File(src_dir);
		if (!file.exists()) {
			// src not available...
			System.err.println(file.getName() + " is not available!");
			return;
		} else if (!file.isDirectory()) {
			zip(zout, file, file.getPath());
			System.err.println(file.getName()
					+ "#is file, not dir!warn, but no error caused!");
		}
		zip(zout, file, "");

		zout.close();
		fos.close();
	}

	/**
	 * 
	 * @param zipOut
	 * @param file
	 * @param base
	 * @throws IOException
	 */
	protected static void zip(ZipOutputStream zipOut, File file, String base)
			throws IOException {
		if (file.isDirectory()) {
			// process dir
			File[] fl = file.listFiles();
			zipOut.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
			base = base.length() == 0 ? "" : base + "/";
			for (int i = 0; i < fl.length; i++) {
				zip(zipOut, fl[i], base + fl[i].getName());
			}
		} else {
			// process file
			zipOut.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
			InputStream in = new FileInputStream(file);
			System.out.println(base);
			int len = 0;
			while ((len = in.read(BYTES)) != -1) {
				zipOut.write(BYTES, 0, len);
			}
			in.close();
		}
	}

	/**
	 * unZip the file
	 * 
	 * @param fileName
	 * @throws IOException
	 */
	public static void unzipFile(String src_zip) throws IOException {
		int i = src_zip.lastIndexOf('.');
		String dest = src_zip.substring(0, i);
		unzipFile(src_zip, dest);
	}

	/**
	 * 
	 * @param src_zip
	 * @param dest_dir
	 * @throws IOException
	 */
	public static void unzipFile(String src_zip, String dest_dir)
			throws IOException {
		ZipUtil.BASE_DIR = dest_dir;
		File base = new File(ZipUtil.BASE_DIR);
		if (!base.exists()) {
			base.mkdirs();
		}
		InputStream in = new BufferedInputStream(new FileInputStream(src_zip));
		ZipInputStream zin = new ZipInputStream(in);
		ZipEntry entry;
		while ((entry = zin.getNextEntry()) != null) {
			try {
				unzip(zin, ZipUtil.BASE_DIR + File.separator + entry.getName());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		zin.close();
	}

	/**
	 * 
	 * @param zin
	 * @param entryName
	 *            the spe file entry in the zip file
	 * @throws IOException
	 */
	protected static void unzip(ZipInputStream zin, String entryName)
			throws IOException {
		System.out.println("unzipping   " + entryName);
		// create dir
		if (entryName.endsWith("/")) {
			System.out.println("INFO: #endsWith/,will return now");
			return;
		}
		int i = entryName.lastIndexOf('/');
		if (i > 0) {
			String path = entryName.substring(0, i);
			File file = new File(path);
			if (!file.exists()) {
				System.out.println(file.getName()
						+ "INFO: #not existed, will create one");
				file.mkdirs();
			}
			System.out.println(path + " =file= " + i);
		}
		// create file
		FileOutputStream out = new FileOutputStream(entryName);
		int len = 0;
		while ((len = zin.read(BYTES)) != -1) {
			out.write(BYTES, 0, len);
		}
		out.close();
	}

}
